package cn.ghf.pet.service.impl;

import cn.ghf.basic.service.impl.BaseServiceImpl;
import cn.ghf.basic.util.PageList;
import cn.ghf.pet.domain.Pet;
import cn.ghf.pet.mapper.PetDetailMapper;
import cn.ghf.pet.mapper.PetMapper;
import cn.ghf.pet.query.PetQuery;
import cn.ghf.pet.service.IPetService;
import cn.ghf.user.domain.User;
import cn.ghf.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageList<Pet> getPets(PetQuery query) {
        return super.queryData(query);
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        //pet表根据ids更新state=0上架
        //更新上架时间
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids );
        map.put("onsaletime", new Date());
        petMapper.onsale(map);
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        //pet表根据ids更新state=0下架
        //更新下架时间
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids );
        map.put("offsaletime", new Date());
        petMapper.offSale(map);
    }

    /**
     * 领养
     * @param id
     * @param loginInfoId
     * @return
     */
    @Override
    public void adopt(Long id, Long loginInfoId) {
        //1.下架 或者已领养
        Pet pet = petMapper.loadById(id);
        pet.setState(0);
        //2.关联领养的用户
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        pet.setUser_id(user.getId());
        //3.更新下架时间
        pet.setOffsaletime(new Date());
        //4.更新pet
        petMapper.update(pet);
    }
}
